import axios, {AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig} from 'axios';
import { ElMessage } from 'element-plus'

const service:AxiosInstance = axios.create({
    baseURL:'/api',
    timeout: 5000
});

service.interceptors.request.use(
    (config: AxiosRequestConfig) => {
        let token=localStorage.getItem('token');
        if (token) {
            if (token && config.headers) {
                config.headers.Authorization =token
            }
        }
        return config;
    },
    (error: AxiosError) => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    (response: AxiosResponse) => {
        if (response.data.code === 200) {
          //  ElMessage.success(response.data.msg)
            return response.data;
        } else {
            
            ElMessage.error(response.data.msg)
            return  Promise.reject(response.data.msg);
        }
    },
    (error: AxiosError) => {
        console.log(error);
        return Promise.reject();
    }
);

export default service;
